predict_table = {
    ('E', '('): ['T', "E'"],
    ('E', 'number'): ['T', "E'"],
    ('E', 'u+'): ['T', "E'"],
    ('E', 'u-'): ['T', "E'"],
    ('E\'', '+'): ['+', 'T', "E'"],
    ('E\'', '-'): ['-', 'T', "E'"],
    ('E\'', ')'): ['ε'],
    ('E\'', '#'): ['ε'],  # 将 '$' 改为 '#'
    ('T', '('): ['F', "T'"],
    ('T', 'number'): ['F', "T'"],
    ('T', 'u+'): ['F', "T'"],
    ('T', 'u-'): ['F', "T'"],
    ('T\'', '*'): ['*', 'F', "T'"],
    ('T\'', '/'): ['/', 'F', "T'"],
    ('T\'', '+'): ['ε'],
    ('T\'', '-'): ['ε'],
    ('T\'', ')'): ['ε'],
    ('T\'', '#'): ['ε'],  # 将 '$' 改为 '#'
    ('F', '('): ['(', 'E', ')'],
    ('F', 'number'): ['number'],
    ('F', 'u+'): ['u+', 'F'],
    ('F', 'u-'): ['u-', 'F']
}

def tokenize(expression):
    i = 0
    tokens = []
    while i < len(expression):
        if expression[i].isspace():
            i += 1
        elif expression[i] in "+-*/()":
            if expression[i] in '+-':
                if i == 0 or expression[i - 1] in "+-*/(":
                    tokens.append('u' + expression[i])  # 一元运算符
                else:
                    tokens.append(expression[i])  # 二元运算符
            else:
                tokens.append(expression[i])
            i += 1
        elif expression[i].isdigit():
            num = 0
            while i < len(expression) and expression[i].isdigit():
                num = num * 10 + int(expression[i])
                i += 1
            tokens.append(('number', num))  # 数字的实际值
    tokens.append('#')  # 将 '$' 改为 '#'
    return tokens

def parser(tokens, predict_table):
    stack = ['#']  # 将 '$' 改为 '#'
    stack.append('E')
    token_iter = iter(tokens)
    current_token = next(token_iter)

    value_stack = []

    def get_token_type(token):
        if isinstance(token, tuple):
            return token[0]
        else:
            return token

    while stack:
        top = stack.pop()
        current_token_type = get_token_type(current_token)
        if top == '#' and current_token == '#':  # 将 '$' 改为 '#'
            break
        elif top == 'ε':
            continue
        elif (top, current_token_type) in predict_table:
            production = predict_table[(top, current_token_type)]
            if production == ['ε']:
                continue
            else:
                stack.extend(reversed(production))
        elif top == current_token_type:
            if isinstance(current_token, tuple) and current_token[0] == 'number':
                value_stack.append(current_token[1])
            current_token = next(token_iter)
        else:
            return "Syntax error"

    # 评估阶段
    token_iter = iter(tokens)
    current_token = next(token_iter)

    def evaluate_E():
        nonlocal current_token
        value = evaluate_T()
        while current_token in ('+', '-'):
            operator = current_token
            current_token = next(token_iter)
            right_value = evaluate_T()
            if operator == '+':
                value += right_value
            else:
                value -= right_value
        return value

    def evaluate_T():
        nonlocal current_token
        value = evaluate_F()
        while current_token in ('*', '/'):
            operator = current_token
            current_token = next(token_iter)
            right_value = evaluate_F()
            if operator == '*':
                value *= right_value
            elif operator == '/':
                if right_value == 0:
                    return "Error: Division by zero"
                value /= right_value
        return value

    def evaluate_F():
        nonlocal current_token
        if current_token == '(':
            current_token = next(token_iter)
            value = evaluate_E()
            if current_token == ')':
                current_token = next(token_iter)
                return value
            else:
                return "Syntax error"
        elif isinstance(current_token, tuple) and current_token[0] == 'number':
            value = current_token[1]
            current_token = next(token_iter)
            return value
        elif current_token == 'u+':
            current_token = next(token_iter)
            return evaluate_F()
        elif current_token == 'u-':
            current_token = next(token_iter)
            return -evaluate_F()
        else:
            return "Syntax error"

    result = evaluate_E()
    return result

def main():
    # 从文件中逐行读取表达式
    with open('input2.txt', 'r', encoding='utf-8') as f:
        for line in f:
            expression = line.strip()
            if not expression:  # 跳过空行
                continue
            print(f"Input: {expression}")
            tokens = tokenize(expression)
            print("Tokens:", tokens)
            result = parser(tokens, predict_table)
            if isinstance(result, str):
                print("Result:", result)
            else:
                print("Result:", result)
            print("-" * 40)

if __name__ == "__main__":
    main()